package org.skydiveapp2;

import org.skydiveapp2.ui.InterfaceManager;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;

public class NotesEditActivity extends Activity
{
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
        
        // init ui
        initializeUI();
        
		overridePendingTransition(R.anim.fadein,
				R.anim.hold);
	}
	
	private void initializeUI()
    {
		// Theme must be set before setContentView
		Bundle extras = getIntent().getExtras();
		// set notes
		String notes = "";
		if (extras != null)
		{
			notes = (extras.getString(Constants.EXTRA_NOTES));
			
			if (extras.containsKey(Constants.EXTRA_SEMI_TRANSPARENT_THEME))
				setTheme(R.style.Theme_SemiTransparent);
		}
		
		// show view
		setContentView(R.layout.notes_view);
		
		// set header text
		if (this.findViewById(R.id.tabletLayout) != null)
		{
			InterfaceManager.getInstance().configureTabletUI(this, 
					R.string.notes_editor_notes_label);
		}
		else
		{
			InterfaceManager.getInstance().configurePhoneUI(this, 
					R.string.notes_editor_notes_label);
		}
		
		EditText nField = (EditText)findViewById(R.id.NotesField);
		nField.setText(notes);
    }
	
	private void save()
    {
            // get notes
            EditText nField = (EditText)findViewById(R.id.NotesField);
            String notes = nField.getText().toString();

            // set result
            Intent intent =  new Intent();
            intent.putExtra(Constants.EXTRA_NOTES, notes);
            setResult(RESULT_OK, intent);
            close();
    }
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.menu_save, menu);
    	inflater.inflate(R.menu.menu_back, menu);
    	return  true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId())
        {
        case R.id.menu_save:
    		save();
            return true;
            
        case R.id.menu_back:
			close();
			overridePendingTransition(R.anim.hold,
					R.anim.fadeout);
            return true;

        default:
            return super.onOptionsItemSelected(item);
        }
    }
    
	@Override
    public void onBackPressed() {
		close();
    }
	
	private void close()
	{
		finish();
    	overridePendingTransition(R.anim.hold,
                R.anim.fadeout);
	}
}
